package com.xy.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xy.config.ScreenRedis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.ConcurrentDateFormat;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("screen-visual")
@Slf4j
@Api(value = "数据大屏", tags = "数据大屏")
public class ScreenController extends BladeController {

	private final BladeRedis bladeRedis;

	/**
	 * 今日人次
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/today-pople-order")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "今日人次", notes = "无参数")
	public R todayPopleOrder() {
		Object data = bladeRedis.hGet(ScreenRedis.SCREEN_PEOPLE_ORDER_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(new Date()));
		if(Func.isEmpty(data)){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("vaule",0);
			return R.data(jsonObject);
		}
		return R.data(data);
	}

	/**
	 * 昨日人次
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/yesterday-pople-order")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "昨日人次", notes = "无参数")
	public R yesterdayPopleOrder() {
		Object data = bladeRedis.hGet(ScreenRedis.SCREEN_PEOPLE_ORDER_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(DateUtil.minusDays(new Date(), 1)));
		if(Func.isEmpty(data)){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("vaule",0);
			return R.data(jsonObject);
		}
		return R.data(data);
	}

	/**
	 * 累计人次
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/pople-order")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "累计人次", notes = "无参数")
	public R PopleOrder() {
		Map<Object,LinkedHashMap<String, Object>> getAll = bladeRedis.hGetAll(ScreenRedis.SCREEN_PEOPLE_ORDER_TOTAL);
		List<Integer> value = new ArrayList<>();
		getAll.entrySet().forEach(o -> {
			LinkedHashMap<String, Object> linkedHashMap = o.getValue();
			value.add(Func.toInt(linkedHashMap.get("value")));
		});
		JSONObject jsonObject = new JSONObject();
		int sum = value.stream().mapToInt(o -> o).sum();
		jsonObject.put("value",sum);
		return R.data(jsonObject);
	}


	/**
	 * 今日人数
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/today-pople-num")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "今日人数", notes = "无参数")
	public R todayPopleNum() {
		Object data = bladeRedis.hGet(ScreenRedis.SCREEN_PEOPLE_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(new Date()));
		if(Func.isEmpty(data)){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("vaule",0);
			return R.data(jsonObject);
		}
		return R.data(data);
	}

	/**
	 * 昨日人数
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/yesterday-pople-num")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "昨日人数", notes = "无参数")
	public R todayPople() {
		Object data = bladeRedis.hGet(ScreenRedis.SCREEN_PEOPLE_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(DateUtil.minusDays(new Date(), 1)));
		if(Func.isEmpty(data)){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("vaule",0);
			return R.data(jsonObject);
		}
		return R.data(data);
	}

	/**
	 * 累计人数
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/pople-num")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "累计人数", notes = "无参数")
	public R popleNum() {


		Map<Object,LinkedHashMap<String, Object>> getAll = bladeRedis.hGetAll(ScreenRedis.SCREEN_PEOPLE_TOTAL);
		List<Integer> value = new ArrayList<>();
		getAll.entrySet().forEach(o -> {
			LinkedHashMap<String, Object> linkedHashMap = o.getValue();
			value.add(Func.toInt(linkedHashMap.get("value")));
		});
		JSONObject jsonObject = new JSONObject();
		int sum = value.stream().mapToInt(o -> o).sum();
		jsonObject.put("value",sum);
		return R.data(jsonObject);
	}


	/**
	 * 当日证件类型
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/pople-type")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "当日证件类型", notes = "无参数")
	public R popleType() {
		List<LinkedHashMap<String, Object>> hashMapList = bladeRedis.hGet(ScreenRedis.SCREEN_PEOPLE_TYPE_TOTAL,ConcurrentDateFormat.of("MM月dd日").format(new Date()));
		JSONObject categories = new JSONObject();
		List<Object> exhibitionName = hashMapList.stream().map(o -> o.get("exhibitionName")).distinct().collect(Collectors.toList());
		categories.put("categories",exhibitionName);
		Map<Object, List<LinkedHashMap<String, Object>>> objectListMap = hashMapList.stream().collect(Collectors.groupingBy(o -> o.get("sortName")));
		JSONArray jsonArray = new JSONArray();
		objectListMap.entrySet().forEach(o -> {
			Object key = o.getKey();
			List<LinkedHashMap<String, Object>> hashMapL = o.getValue();
			List<Object> listSum = hashMapL.stream().map(or -> or.get("num")).collect(Collectors.toList());
			JSONObject object = new JSONObject();
			object.put("name",key);
			object.put("data",listSum);
			jsonArray.add(object);
		});
		categories.put("series",jsonArray);
		return R.data(categories);
	}

	/**
	 * 历史日期人次人数
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/histroy-pople")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "历史日期人次人数", notes = "无参数")
	public R histroyPople() {
		Map<String, LinkedHashMap<String, Object>> mapAll = bladeRedis.hGetAll(ScreenRedis.SCREEN_PEOPLE_TOTAL);
		LinkedHashMap<String, LinkedHashMap<String, Object>> collect1 = mapAll.entrySet().stream().sorted(Map.Entry.comparingByKey(String::compareTo)).
			collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));

		List<Object> collect = collect1.entrySet().stream().map(o -> o.getKey()).collect(Collectors.toList());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("categories",collect);

		List<Object> collectsum = collect1.entrySet().stream().map(o -> o.getValue().get("value")).collect(Collectors.toList());
		JSONArray jsonArray = new JSONArray();
		JSONObject json = new JSONObject();
		json.put("name","人数");
		json.put("data",collectsum);
		jsonArray.add(json);


		Map<String, LinkedHashMap<String, Object>> mapAllOrder = bladeRedis.hGetAll(ScreenRedis.SCREEN_PEOPLE_ORDER_TOTAL);

		LinkedHashMap<String, LinkedHashMap<String, Object>> collect2 = mapAllOrder.entrySet().stream().sorted(Map.Entry.comparingByKey(String::compareTo)).
			collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));

		List<Object> collectsumOrder = collect2.entrySet().stream().map(o -> o.getValue().get("value")).collect(Collectors.toList());
		JSONObject jsonOrder = new JSONObject();
		jsonOrder.put("name","人次");
		jsonOrder.put("data",collectsumOrder);
		jsonArray.add(jsonOrder);

		jsonObject.put("series",jsonArray);
		return R.data(jsonObject);
	}

	/**
	 * 当年龄段统计
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/pople-age")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "当年龄段统计", notes = "无参数")
	public R popleAge() {
		Object data = bladeRedis.hGet(ScreenRedis.SCREEN_PEOPLE_AGE, ConcurrentDateFormat.of("MM月dd日").format(new Date()));
		return R.data(data);
	}

	/**
	 * 黑名单
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/pople-back")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "黑名单", notes = "无参数")
	public R popleBack() {
		Object data = bladeRedis.hGet(ScreenRedis.SCREEN_BACK_LIST, ConcurrentDateFormat.of("MM月dd日").format(new Date()));
		return R.data(data);
	}

	/**
	 * 时间段
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/pople-time")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "时间段", notes = "无参数")
	public R popleTime() {
		List<LinkedHashMap<String, Object>> hashMapList  = bladeRedis.hGet(ScreenRedis.SCREEN_PEOPLE_TIME, ConcurrentDateFormat.of("MM月dd日").format(new Date()));

		List<Object> time = hashMapList.stream().map(o -> o.get("time")).distinct().collect(Collectors.toList());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("categories",time);


		List<Object> collectsum = hashMapList.stream().map(o -> o.get("num")).collect(Collectors.toList());
		JSONArray jsonArray = new JSONArray();
		JSONObject json = new JSONObject();
		json.put("name","今日");
		json.put("data",collectsum);
		jsonArray.add(json);


		List<LinkedHashMap<String, Object>> mapAllOrder = bladeRedis.hGet(ScreenRedis.SCREEN_PEOPLE_TIME, ConcurrentDateFormat.of("MM月dd日").format(DateUtil.minusDays(new Date(), 1)));
		List<Object> collectsumOrder = mapAllOrder.stream().map(o -> o.get("num")).collect(Collectors.toList());
		JSONObject jsonOrder = new JSONObject();
		jsonOrder.put("name","昨日");
		jsonOrder.put("data",collectsumOrder);
		jsonArray.add(jsonOrder);

		jsonObject.put("series",jsonArray);

		return R.data(jsonObject);
	}
}
